package cn.com.haopy.yue.ai.admin.web;


import cn.com.haopy.yue.ai.admin.annotation.RequiresPermissionsDesc;
import cn.com.haopy.yue.ai.core.utils.ResponseUtil;
import cn.com.haopy.yue.ai.core.validator.Order;
import cn.com.haopy.yue.ai.core.validator.Sort;
import cn.com.haopy.yue.ai.db.entity.arisk.AIRiskAd;
import cn.com.haopy.yue.ai.db.service.hos.AIRiskAdService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;

/**
 * @Description: TODO
 * @Author DuTao
 * @Date 2023/9/25 14:50
 */
@RestController
@RequestMapping("/admin/ad")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "广告")
public class AdminAdController {
    private final Log logger = LogFactory.getLog(AdminAdController.class);

    @Autowired
    private AIRiskAdService adService;

    @RequiresPermissions("admin:ad:list")
    @RequiresPermissionsDesc(menu = {"运营管理", "广告管理"}, button = "查询")
    @GetMapping("/list")
    public Object list(String name, String content,
                       Integer position,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer limit,
                       @Sort @RequestParam(defaultValue = "create_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order) {
        Page<AIRiskAd> adList = adService.page(name, page, limit, sort, order);
        return ResponseUtil.okPage(adList);
    }

    @RequiresPermissions("admin:ad:create")
    @RequiresPermissionsDesc(menu = {"运营管理", "广告管理"}, button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody AIRiskAd ad) {
        Object error = validate(ad);
        if (error != null) {
            return error;
        }
        adService.save(ad);
        return ResponseUtil.ok(ad);
    }

    private Object validate(AIRiskAd ad) {
        String name = ad.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseUtil.badArgument();
        }
        return null;
    }

    @RequiresPermissions("admin:ad:read")
    @RequiresPermissionsDesc(menu = {"运营管理", "广告管理"}, button = "详情")
    @GetMapping("/read")
    public Object read(@NotNull Integer id) {
        AIRiskAd ad = adService.getById(id);
        return ResponseUtil.ok(ad);
    }

    @RequiresPermissions("admin:ad:update")
    @RequiresPermissionsDesc(menu = {"运营管理", "广告管理"}, button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody AIRiskAd ad) {
        Object error = validate(ad);
        if (error != null) {
            return error;
        }
        if (!adService.updateById(ad)) {
            return ResponseUtil.updatedDataFailed();
        }
        return ResponseUtil.ok(ad);
    }

    @RequiresPermissions("admin:ad:delete")
    @RequiresPermissionsDesc(menu = {"运营管理", "广告管理"}, button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody AIRiskAd ad) {
        Integer id = ad.getId();
        if (id == null) {
            return ResponseUtil.badArgument();
        }
        adService.removeById(id);
        return ResponseUtil.ok();
    }


    @GetMapping("/listByPosition")
    public Object listByPosition(@RequestParam @ApiParam("位置：0 首页，1 收费页") Short position) {
        return ResponseUtil.ok(adService.listByPosition(position));
    }
}
